package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoForm;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 课程和课程简介
 * </p>
 *
 * @author atguigu
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
@Api(description="课程添加")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     *添加课程
     * 传入的courseInfo类包括课程表和课程简介表
     */
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoForm courseInfo){
        String cid=eduCourseService.addCourseInfoForm(courseInfo);
        return R.ok().data("courseId",cid);
    }

    /**
     *根据课程id获取 课程表 信息和 课程描述表 中的信息
     * 返回值为 CourseInfoForm
    **/
    @GetMapping("getCourseInfoById/{courseId}")
    public R getAllCourseInfo(@PathVariable String courseId){
        CourseInfoForm courseInfo =  eduCourseService.getAllCourseInfoByCourseId(courseId);

        return R.ok().data("courseInfo",courseInfo);
    }

    /**
     * 课程修改 课程表 和 课程描述表 中的信息
     *
    **/
    @PostMapping("updateCourseInfoForm")
    public R updateCourseInfoForm(@RequestBody CourseInfoForm courseInfoForm){
        eduCourseService.updateCourseInfoForm(courseInfoForm);
       return R.ok();
    }

    /**
     *获取最终发布的课程信息
     *
     */
    @GetMapping("getCoursePublishVo/{id}")
    public R getCoursePublishVo(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.getCoursePublish(id);

        return R.ok().data("coursePublishVo",coursePublishVo);
    }

    /**
     * 根据课程Id删除课程的所有信息
     **/
    @DeleteMapping("deleteCourseAllInfoById/{courseId}")
    public R deleteCourseAllInfoById(@PathVariable String courseId){
        eduCourseService.deleteAllInfoByCourseId(courseId);
        return R.ok();
    }

    /**
     *
     * 课程最终发布
     * 要修改课程的状态为Normal
    **/
    @PostMapping("finalCoursePublish/{id}")
    public R finalCoursePublish(@PathVariable String id){

        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//课程最终发布
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    /**
     *
     * 获取课程最终发布的列表
    **/
    @GetMapping("getAllList")
    public R getAllList(){
        List<EduCourse> eduCourses = eduCourseService.list(null);
        return R.ok().data("eduCourses",eduCourses);
    }

    /**
     * 实现带分页的课程查询条件 具体可参考教师的带分页查询
     * pageListCourseCondition
     * current :当前页
     * limit :每页的限制数
    **/
    @PostMapping("pageListCourseCondition/{current}/{limit}")
    public R pageListCourseCondition(
            @RequestBody(required = false) CourseQuery courseQuery,
            @PathVariable long current,
            @PathVariable long limit
    ){
        //1.查询条件
        //拿到从前端传进来的值
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        //构造查询条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        //按时间来排序
        wrapper.orderByDesc("gmt_create");

        //2. 构造分页对象
        Page<EduCourse> EduCoursePage = new Page<>(current, limit);


        eduCourseService.page(EduCoursePage, wrapper);

        //然后根据EduCoursePage获取记录和查询出来的总数
        List<EduCourse> eduCourseList = EduCoursePage.getRecords();
        long total = EduCoursePage.getTotal();


        return R.ok().data("eduCourses",eduCourseList).data("rows",total);
    }













}

